package com.geekchique.runningbuddy.view;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.text.Layout.Alignment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.Today;
import com.geekchique.runningbuddy.controller.PastJogsControllerImpl;
import com.geekchique.runningbuddy.model.PastJogsCalendar;
import com.geekchique.runningbuddy.provider.ActivityID;

public class PastjogsView  implements ViewFactory {

	private Activity activity;
	private Button yearBack;
	private Button monthBack;
	private Button dayBack;
	private Button dayForward;
	private Button monthForward;
	private Button yearForward;
	private LinkedHashMap<Integer, Button> tableOfCalendarDatesView;
	private LinkedHashMap<Integer, Integer> tableOfCalendarDatesModel;
	private ArrayList<Integer> joggingDaysInThisMonth;
	private PastJogsControllerImpl pastJogsController;
	private TextView currentMonth;
	private Integer indexOfTodayCell;
	
    public static final int NOTIFICATION_ID = 0x1337;
    public static final int IMAGESUBACTIVITY_ID = 0x7331;
    
    public static final String DRAWABLE_RESID_MARKER = "DRAWABLE_RESID_MARKER";
    public static final String CAPTION_TO_SHOW_MARKER = "CAPTION_TO_SHOW_MARKER";
	
	public PastjogsView(Activity activity,
			PastJogsControllerImpl pastJogsViewController,
			Button gotoToday,
			Button yearBack,
			Button monthBack,
			Button dayBack,
			TextView currentMonth,
			Button dayForward,
			Button monthForward, 
			Button yearForward) {
		
		this.activity = activity;
		this.pastJogsController = pastJogsViewController;
		this.yearBack = yearBack; 
		this.monthBack = monthBack; 
		this.dayBack = dayBack; 
        
		this.currentMonth = currentMonth;
		
		this.dayForward = dayForward; 
		this.monthForward = monthForward; 
		this.yearForward = yearForward;
		
        gotoToday.setOnClickListener(gotoTodayListener);
		
		setYearBackListener(yearBackListener);
		setMonthBackListener(monthBackListener);
		setDayBackListener(dayBackListener);
		setDayForwardListener(dayForwardListener);
		setMonthForwardListener(monthForwardListener);
		setYearForwardListener(yearForwardListener);
		
	}

	public void updateCalendarView(PastJogsCalendar pastJogsCalendar) {
		tableOfCalendarDatesModel = pastJogsCalendar.getDaysInMonthTable();
		currentMonth.setText(pastJogsCalendar.getCurrentShowingMonth());
		joggingDaysInThisMonth = new ArrayList<Integer>();
		
		for(int currentRow=0; currentRow <= pastJogsCalendar.NO_OF_ROWS; currentRow++){
			
			for(int currentCol=0; currentCol <= pastJogsCalendar.NO_OF_COLS; currentCol++){
				updateCalendarViewCellWithCalendarModelCell(currentCol, currentRow);
			}
		}
	}

	private void updateCalendarViewCellWithCalendarModelCell(int currentCol, int currentRow) {
		String tableIndex = new String(String.valueOf(currentRow) + String.valueOf(currentCol));
		
		Button cellInView = (Button)tableOfCalendarDatesView.get(Integer.valueOf(tableIndex));
		Integer cellInModel = tableOfCalendarDatesModel.get(Integer.valueOf(tableIndex));
		
		cellInView.setText(cellInModel.toString());
		Integer indexOfInsertion = Integer.valueOf(tableIndex);
		tableOfCalendarDatesView.put(indexOfInsertion, (Button)cellInView);
		
	}
	

	public void setCellsInCalendarTable(LinkedHashMap<Integer, Button> tableCells) {
		this.tableOfCalendarDatesView = tableCells;
	}

	public void setYearBackListener(OnClickListener yearBackListener) {
		yearBack.setOnClickListener(yearBackListener);
	}

	public void setMonthBackListener(OnClickListener monthBackListener) {
		monthBack.setOnClickListener(monthBackListener);
	}

	public void setDayBackListener(OnClickListener dayBackListener) {
		dayBack.setOnClickListener(dayBackListener);
	}

	public void setMonthForwardListener(OnClickListener monthForwardListener) {
		monthForward.setOnClickListener(monthForwardListener);
		
	}

	public void setYearForwardListener(OnClickListener yearForwardListener) {
		yearForward.setOnClickListener(yearForwardListener);
	}

	public void setDayForwardListener(OnClickListener dayForwardListener) {
		dayForward.setOnClickListener(dayForwardListener);
	}


	private OnClickListener yearBackListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.YEAR, -1);
		}
	};
	private OnClickListener monthBackListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.MONTH, -1);
		}
	};
	private OnClickListener dayBackListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.DAY_OF_MONTH, -1);
		}
	};
	private OnClickListener dayForwardListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.DAY_OF_MONTH, 1);
		}
	};
	private OnClickListener monthForwardListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.MONTH, 1);
		}
	};
	private OnClickListener yearForwardListener = new OnClickListener() {
		public void onClick(View v) {
			pastJogsController.ammendCurrentlyShowingDate(Calendar.YEAR, 1);
		}
	};
	
	private OnClickListener gotoTodayListener = new OnClickListener() {
	  	public void onClick(View v) {
	  		switchToToday();
	    }
	};
	  
	private void switchToToday() {
	    Intent i = new Intent(activity, Today.class);
	    activity.startSubActivity(i, ActivityID.TODAYJOGS);	
	}

	public View makeView() {
	    TextView textView = new TextView(activity);
	    textView.setAlignment(Alignment.ALIGN_CENTER);
	    textView.setTextSize(50);
	    return textView;
	}

	public void setCurrentlySelectedDay(Integer index) {
		Button cellInView = (Button)tableOfCalendarDatesView.get(index);
		cellInView.setBackground(activity.getResources().getDrawable(R.color.solid_yellow));
		indexOfTodayCell = index;
	}
	
	public void setAHighightedDayOfJogging(Integer index) {
		Button cellInView = (Button)tableOfCalendarDatesView.get(index);
		cellInView.setBackground(activity.getResources().getDrawable(R.color.solid_red));
		joggingDaysInThisMonth.add(index);
	}

	public void removeHighlightOnTodayCellInTable() {
		Button cellInView = (Button)tableOfCalendarDatesView.get(indexOfTodayCell);
		cellInView.setBackground(activity.getResources().getDrawable(R.color.calcell_background));		
	}

	public void addDayClickListeners(LinkedHashMap<Integer, Button> tableCells) {
		
		for(Iterator<Entry<Integer, Button>> iter = tableCells.entrySet().iterator(); iter.hasNext();){
			Entry<Integer, Button> entry = iter.next();
			final Button cell = (Button) entry.getValue();
			final Integer position = (Integer) entry.getKey();

			cell.setOnClickListener(clickADay);
			tableOfCalendarDatesView.put(position, cell);
		}		
		
	}
	
    public OnClickListener clickADay = new OnClickListener(){
        public void onClick(View aView) {

        	Button clickedButton = (Button)aView;
            String buttonCaption = clickedButton.getText().toString();

             pastJogsController.loadDayDetails(buttonCaption);
        }
  };

	public void removeAllCellHighlights() {
		
		for(Iterator<Entry<Integer, Button>>  iter = tableOfCalendarDatesView.entrySet().iterator(); iter.hasNext();){
				Entry<Integer, Button> cell =  iter.next();
				cell.getValue().setBackground(activity.getResources().getDrawable(R.color.calcell_background));
				tableOfCalendarDatesView.put(cell.getKey(), cell.getValue());
		}
		
	}

}
